<?php
class Controller_Services_Ebaycase extends Controller_Abstract
{
	function actionGetCase()
	{
		$this->_view['title'] = '更新纠纷';
		$this->_view['shop'] = Shop_User_Mapping::getShopByPlatform();
		if ($this->_context->isPOST())
		{
			if (isset($_POST['shop_ids']))
			{
				$shop_ids = $_POST['shop_ids'];
				$mapping = Shop_User_Mapping::checkMapping($shop_ids);
				if ($mapping['ack'] == SUCCESS)
				{
					$start_time = isset($_POST['begin_create_time']) ? date('c', strtotime($_POST['begin_create_time'])) : date('c', CURRENT_TIMESTAMP);
					$end_time = isset($_POST['end_create_time']) ? date('c', strtotime($_POST['end_create_time'] . ' 23:59:59')) : date('c', CURRENT_TIMESTAMP);
					$result = Services_Ebay_Case::getUserCases($shop_ids, $start_time, $end_time);
					if ($result['ack'] == SUCCESS)
					{
						return $this->_redirectMessage('更新成功', '正在返回列表', url('Services::Ebaycase/List'), $result['ack']);
					}
					else
					{
						return $this->_redirectMessage('更新失败', '返回列表..', $_SERVER['HTTP_REFERER'], $result['ack']);
					}
				}
				else
				{
					return $this->_redirectMessage('更新失败', $mapping['message'], $_SERVER['HTTP_REFERER'], $mapping['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('更新失败', '请选择店铺，正在返回..', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}

	function actionList()
	{
		$this->_view['title'] = '纠纷列表';
		$ps = isset($_GET['ps']) ? $_GET['ps'] : '20';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : '1';
		$this->_view['shop'] = Shop_User_Mapping::getShopByPlatform();
		$condition = Helper_BSS_Normal::buildCondition();
		$condition['shop_user_mapping'] = Shop_User_Mapping::getShopIdByUserId();
		$result = Services_Ebay_Case::search($condition, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Page($result['pagination'], url('Services::EbayCase/List', $condition));
			$this->_view['page'] = $page->getPage();
			$this->_view['case'] = $result['data'];
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '详细';
		if (isset($_GET['id']))
		{
			$case = Services_Ebay_Case::find('services_ebay_case.id=?', $_GET['id'])->asArray()->getOne();
			if ($case)
			{
				$mapping = Shop_User_Mapping::checkMapping($case['shop_id']);
				if ($mapping['ack'] == SUCCESS)
				{
					if ($case['status'] != 'CLOSED')
					{
						$result = Services_Ebay_Case::getEBPCaseDetail($case);
					}
					$case = Services_Ebay_Case::find('services_ebay_case.id=?', $_GET['id'])
						->joinLeft('shop', 'name as shop_name', 'shop.id=services_ebay_case.shop_id')
						->joinLeft('currency', 'symbol as currency_symbol', 'currency.id=services_ebay_case.currency_id')
						->asArray()->getOne();
					$this->_view['case'] = $case;
					$listing = Online_Ebay_Listing::find('item_id=?', $case['shop_item_id'])
						->joinLeft('online_ebay_listing_item', 'start_price as item_start_price, sku', 'online_ebay_listing_item.online_ebay_listing_id=online_ebay_listing.id')
						->joinLeft('product_basic', 'number as sku', 'product_basic.number=online_ebay_listing_item.sku')
						->joinLeft('currency', 'symbol as currency_symbol', 'currency.id=online_ebay_listing.currency_id')
						->asArray()->getAll();
					Helper_BSS_Normal::getProductImgUrl($listing, 'sku');
					$this->_view['listing'] = $listing;
					$response = Services_Ebay_Case_Response::find('services_ebay_case_id=?', $case['id'])->order('response_time desc')->asArray()->getAll();
					$this->_view['response'] = $response;
					$message_template = Services_Message_Template::find('create_user_id=?', CURRENT_USER_ID)->setColumns('id, name')->asArray()->getAll();
					$this->_view['message_template'] = $message_template;
					$order = Sales_Order_customer_info::find('sales_order_customer_info.shop_buyer_id=? and sales_order.trash_status=?', $case['buyer'], Q::ini('custom_flag/sales_order_trash_status/normal/value'))
						->joinLeft('sales_order', 'sales_order.*', 'sales_order.id=sales_order_customer_info.sales_order_id')
						->joinLeft('currency', 'symbol', 'currency.id=sales_order.currency_id')
						->joinLeft('shipping_method', 'name as shipping_name', 'shipping_method.id=shipping_method')
						->order('sales_order.sales_time desc')->asArray()->getAll();
					$this->_view['order'] = array_slice($order, 0, 5);
				}
				else
				{
					return $this->_redirectMessage('操作失败', $mapping['message'], url('Services::EbayCase/List'), $mapping['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('操作失败', '找不到纠纷信息，正在返回..', url('Services::EbayCase/List'), FAILURE);
			}
		}
	}
	
	function actionMessageTemplateAjax()
	{
		$this->_view['title'] = '邮件模板内容';
		$message_template = Services_Message_Template::find('id=?', $_REQUEST['id'])->asArray()->getOne();
		echo $message_template['body'];
		exit;
	}
	
	function actionSolution()
	{
		$this->_view['title'] = '解决方案';
		if (isset($_POST['solution']) && isset($_POST['services_ebay_case_id']))
		{
			$case = Services_Ebay_Case::find('id=?', $_POST['services_ebay_case_id'])->asArray()->getOne();
			$mapping = Shop_User_Mapping::checkMapping($case['shop_id']);
			if ($mapping['ack'] == SUCCESS)
			{
				if ($case['status'] != 'CLOSED')
				{
					$method = Helper_BSS_Normal::getIniOne('services_ebay_case_response_solution', 'value', $_POST['solution'], 'method');
					$result = Services_Ebay_Case_Response::$method($_POST);
					if ($result['ack'] == SUCCESS)
					{
						return $this->_redirectMessage('提交成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
					}
					else
					{
						return $this->_redirectMessage('提交失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
					}
				}
				else
				{
					return $this->_redirectMessage('提交失败', '纠纷状态已经关闭，不需要回应', $_SERVER['HTTP_REFERER'], FAILURE);
				}
			}
			else
			{
				return $this->_redirectMessage('提交失败', $mapping['message'], url('Services::EbayCase/List'), $mapping['ack']);
			}
		}
	}
}